package com.example.student.controller;

import com.example.student.common.Result;
import com.example.student.entity.SysNotification;
import com.example.student.model.NotificationVO;
import com.example.student.model.PageResult;
import com.example.student.service.NotificationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/api/notifications")
public class NotificationController {

    @Resource
    private NotificationService notificationService;

    /**
     * 获取通知列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('notification:list')")
    public Result<PageResult<NotificationVO>> list(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return Result.success(notificationService.getNotificationList(title, type, pageNum, pageSize));
    }

    /**
     * 获取通知详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('notification:view')")
    public Result<NotificationVO> getById(@PathVariable Long id) {
        return Result.success(notificationService.getNotificationById(id));
    }

    /**
     * 获取未读通知数量
     */
    @GetMapping("/unread/count")
    public Result<Integer> getUnreadCount() {
        return Result.success(notificationService.getUnreadCount());
    }

    /**
     * 标记通知为已读
     */
    @PutMapping("/read/{id}")
    @PreAuthorize("hasAuthority('notification:view')")
    public Result<Void> markAsRead(@PathVariable Long id) {
        notificationService.markAsRead(id);
        return Result.success();
    }

    /**
     * 标记所有通知为已读
     */
    @PutMapping("/read/all")
    @PreAuthorize("hasAuthority('notification:view')")
    public Result<Void> markAllAsRead() {
        notificationService.markAllAsRead();
        return Result.success();
    }

    /**
     * 添加通知
     */
    @PostMapping
    @PreAuthorize("hasAuthority('notification:add')")
    public Result<Void> add(@Valid @RequestBody SysNotification notification) {
        notificationService.addNotification(notification);
        return Result.success();
    }

    /**
     * 更新通知
     */
    @PutMapping
    @PreAuthorize("hasAuthority('notification:edit')")
    public Result<Void> update(@Valid @RequestBody SysNotification notification) {
        notificationService.updateNotification(notification);
        return Result.success();
    }

    /**
     * 删除通知
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('notification:delete')")
    public Result<Void> delete(@PathVariable Long id) {
        notificationService.deleteNotification(id);
        return Result.success();
    }
} 